﻿using System.IO;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using ExtendedMvc.Tests.Model;
using ExtendedMvc.Tests.Model.Entities;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ExtendedMvc.Tests {


	/// <summary>
	///This is a test class for FormTest and is intended
	///to contain all FormTest Unit Tests
	///</summary>
	[TestClass()]
	public class HtmlFormTest {

		private static DummyDataSource database;
		private static ModelViewPage<Dummy> view;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext { get; set; }

		#region Additional test attributes
		// 
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		[ClassInitialize()]
		public static void MyClassInitialize(TestContext testContext) {
			database = new DummyDataSource();
			var dummy = database.Dummies.All().First();
			view = MvcMockHelpers.FakeViewPage("Index", dummy);
			if (!RouteTable.Routes.Any()) {
				RouteTable.Routes.MapRoute(
					"Default", // Route name
					"{controller}/{action}/{id}", // URL with parameters
					new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
				);
			}
		}

		//Use ClassCleanup to run code after all tests in a class have run
		//[ClassCleanup()]
		//public static void MyClassCleanup() {
		//}

		//Use TestInitialize to run code before running each test
		[TestInitialize()]
		public void MyTestInitialize() {
			view.ViewContext.ViewData.ModelState.Clear();
			view.ViewContext.Writer.Close();
			view.ViewContext.Writer = new StringWriter();
		}

		//Use TestCleanup to run code after each test has run
		//[TestCleanup()]
		//public void MyTestCleanup() {
		//}

		#endregion

		/// <summary>
		///A test for Form Constructor
		///</summary>
		[TestMethod()]
		public void HtmlForm_NoParameters() {
			// Arrange
			var outputUrl = "/Home/Index.aspx";

			// Act
			var form = view.Html.Form();
			var expectedOutput = string.Format(@"<form action=""{0}"" id=""{1}"" method=""post"">", outputUrl, form.Id);
			var output = view.ViewContext.Writer.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for Form Constructor
		///</summary>
		[TestMethod()]
		public void HtmlForm_WithFormAction() {
			// Arrange
			var formAction = "~/link/dummy.aspx";
			var outputUrl = "/link/dummy.aspx";

			// Act
			var form = view.Html.Form(formAction);
			var expectedOutput = string.Format(@"<form action=""{0}"" id=""{1}"" method=""post"">", outputUrl, form.Id);
			var output = view.ViewContext.Writer.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for Form Constructor
		///</summary>
		[TestMethod()]
		public void HtmlForm_WithFormMethod() {
			// Arrange
			var formMethod = FormMethod.Get;
			var outputUrl = "/Home/Index.aspx";

			// Act
			var form = view.Html.Form(method: formMethod);
			var expectedOutput = string.Format(@"<form action=""{0}"" id=""{1}"" method=""get"">", outputUrl, form.Id);
			var output = view.ViewContext.Writer.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for Form Constructor
		///</summary>
		[TestMethod()]
		public void HtmlActionForm_NoParameters() {
			// Arrange
			var outputUrl = "/";

			// Act
			var form = view.Html.ActionForm();
			var expectedOutput = string.Format(@"<form action=""{0}"" id=""{1}"" method=""post"">", outputUrl, form.Id);
			var output = view.ViewContext.Writer.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for Form Constructor
		///</summary>
		[TestMethod()]
		public void HtmlRouteForm_NoParameters() {
			// Arrange
			var outputUrl = "/";

			// Act
			var form = view.Html.RouteForm();
			var expectedOutput = string.Format(@"<form action=""{0}"" id=""{1}"" method=""post"">", outputUrl, form.Id);
			var output = view.ViewContext.Writer.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}
	}
}
